<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/auth'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
  type: '分析师'
})

const loading = ref(false)
const errorMessage = ref('')

const validatePassword = () => {
  if (form.value.password !== form.value.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return false
  }
  return true
}

const handleRegister = async () => {
  if (!form.value.username || !form.value.password || !form.value.confirmPassword) {
    errorMessage.value = '请填写完整信息'
    return
  }

  if (!validatePassword()) return

  try {
    loading.value = true
    errorMessage.value = ''
    
    await authStore.register(form.value)
    router.push('/login')
  } catch (error) {
    errorMessage.value = error.response?.data?.message || '注册失败'
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="auth-page">
    <el-card class="auth-card">
      <template #header>
        <div class="auth-header">
          <h2>注册</h2>
        </div>
      </template>
      
      <el-form :model="form" label-width="100px" @submit.prevent="handleRegister">
        <el-form-item label="用户名">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
        </el-form-item>
        
        <el-form-item label="确认密码">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" show-password />
        </el-form-item>
        
        <el-form-item label="用户类型">
          <el-select v-model="form.type" placeholder="请选择用户类型">
            <el-option label="管理员" value="管理员" />
            <el-option label="分析师" value="分析师" />
            <el-option label="审计员" value="审计员" />
          </el-select>
        </el-form-item>
        
        <div v-if="errorMessage" class="error-message">
          <el-alert :title="errorMessage" type="error" show-icon />
        </div>
        
        <el-form-item>
          <el-button type="primary" :loading="loading" native-type="submit" class="auth-button">
            注册
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="auth-footer">
        <span>已有账号？</span>
        <router-link to="/login">立即登录</router-link>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.auth-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.auth-card {
  width: 500px;
  padding: 20px;
}

.auth-header {
  text-align: center;
  margin-bottom: 20px;
}

.error-message {
  margin-bottom: 20px;
}

.auth-button {
  width: 100%;
}

.auth-footer {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.auth-footer a {
  color: #409EFF;
  text-decoration: none;
  margin-left: 5px;
}
</style>